package day01;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/11 16:50
 * 二维数组中的查找
 */
public class Offer04 {

    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if (matrix == null || matrix.length <=0 || matrix[0].length <= 0) {
            return false;
        }

        // 行
        int rows = matrix.length;
        // 列
        int cols = matrix[0].length;

        int row = rows - 1;
        int col = 0;

        while(row >= 0 && col < cols) {
            if (target > matrix[row][col]) {
                col++;
            } else if (target < matrix[row][col]){
                row--;
            } else {
                return true;
            }
        }
        return false;
    }
}
